package com.bnzj.cloud.gateway.config;

import com.bnzj.cloud.gateway.external.*;
import com.bnzj.cloud.gateway.external.converter.PhoneTokenConverter;
import com.bnzj.cloud.gateway.external.handler.ThirdSysTokenInfoHandler;
import com.bnzj.cloud.gateway.external.matcher.PhoneTokenMatcher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @ClassName CustomTokenConfiguration
 * @Description TODO
 * @Author
 * @Date 2020/3/14
 * @Version V1.0
 **/
@Configuration
public class CustomTokenConfiguration {
    @Bean
    public CustomTokenParseHandler phoneTokenHandler(){
        return new ThirdSysTokenInfoHandler();
    }

    @Bean
    public CustomTokenAuthenticationWebFilter thirdSysTokenAuthenticationWebFilter(){
        CustomTokenAuthenticationWebFilter phoneFilter =  new CustomTokenAuthenticationWebFilter(new CustomReactiveAuthenticationManager());
        phoneFilter.setRequiresAuthenticationMatcher(phoneTokenExchangeMatcher());
        phoneFilter.setServerAuthenticationConverter(phoneConverter());
        return phoneFilter;
    }

    @Bean
    public PhoneTokenMatcher phoneTokenMatcher(){
        return new PhoneTokenMatcher();
    }

    @Bean
    public CustomTokenServerWebExchangeMatcher phoneTokenExchangeMatcher(){
        CustomTokenServerWebExchangeMatcher phoneTokenExchangeMatcher = new CustomTokenServerWebExchangeMatcher();
        phoneTokenExchangeMatcher.setCustomTokenMatcher(phoneTokenMatcher());
        return phoneTokenExchangeMatcher;
    }

    @Bean
    public CustomTokenAuthenticationConverter phoneConverter(){
        CustomTokenAuthenticationConverter converter = new CustomTokenAuthenticationConverter();
        converter.setCustomTokenConverter(phoneTokenConverter());
        converter.setTokenParseHandler(phoneTokenHandler());
        return converter;
    }

    @Bean
    public PhoneTokenConverter phoneTokenConverter(){
        return new PhoneTokenConverter();
    }

}
